package com.phychips.rcp;

import android.os.Environment;
import android.util.Log;

import java.io.IOException;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class RcpLogger
{
    private FileOutputStream fos;
    private boolean bFileOut = false;
    private String strFileName;

    public String getStrFileName()
    {
	return strFileName;
    }

    public void setStrFileName(String strFileName)
    {
	this.strFileName = strFileName;
    }

    public void setFileOut(boolean bFile, String str)
    {
	bFileOut = bFile;
	SimpleDateFormat mDateFormat = new SimpleDateFormat("yyMMdd-HHmmss",
		Locale.KOREA);
	Date currentTime = new Date();
	setStrFileName(str + "_" + mDateFormat.format(currentTime) + ".txt");
    }

    public void SetFileOutput(boolean bFile, String filename)
    {
	bFileOut = bFile;
	if (bFile)
	{
	    File path = new File(Environment.getExternalStorageDirectory()
		    + "/AE100/");

	    if (!path.exists())
	    {
		path.mkdirs();
		// Log.i("logfile", "make dir:" + path.getAbsolutePath());
	    }

	    try
	    {
		File file = new File(path, filename);
		fos = new FileOutputStream(file);
		bFile = true;
		Log.i("logfile", "open");
	    }
	    catch (IOException e)
	    {
		Log.i("logfile", "err");
		bFile = false;
		e.printStackTrace();
	    }
	}

    } // End SetFileOutput()

    public void FileOutputRelease()
    {
	if (bFileOut)
	{
	    try
	    {
		fos.close();
	    }
	    catch (IOException e)
	    {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	}

    } // End FileOutputRelease()

    public void toFileOutput(short DATA)
    {
	// if (bFileOut)
	{
	    try
	    {
		String str = (Integer.toString(DATA) + "\n");
		fos.write(str.getBytes());
		fos.flush();
		// dos.writeBytes(str);
		// dos.flush();
		str = null;
	    }
	    catch (IOException e)
	    {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	}

    } // End toFileOutput()

}